package org.zhongyin.testing;

import java.io.IOException;
import java.util.Arrays;

import org.zhongyin.Regression.Regression;
import org.zhongyin.Util.Utility;
import org.zhongyin.internal.core.ReadExcel;

import Jama.Matrix;

public class TestingExcel {
	
	public Matrix T_24_168_2010(int hour) throws IOException{
		ReadExcel Original = new ReadExcel();
		Original.setInputFile("c:/CoolingLoadProject/All result1.xls");
		
	    double[][] original_data = Original.readT24_168(hour);
	    
	    
	    
	    int length_origianl = original_data.length;
	    double[] array_Y = new double[length_origianl];
	    double[][] array_X = new double[length_origianl][17];
	    
	    
	    
	    for(int i = 0; i < length_origianl; i++){
	    	
	    		array_Y[i] = original_data[i][0];
	    	
	    	for(int j = 0; j < 11; j++){	
	    		array_X[i][j] = original_data[i][j+1];
	    	}
	    	
	    	
	    }
	    
	    for(int i = 0; i < length_origianl; i++){
	    		//t*t
	    		array_X[i][11] = original_data[i][6] * original_data[i][6];
	    		//t24*t24
	    		array_X[i][12] = original_data[i][7] * original_data[i][7];
	    		//t168*t168
	    		array_X[i][13] = original_data[i][8] * original_data[i][8];
	    		//t * RHt
	    		array_X[i][14] = original_data[i][6] * original_data[i][9];
	    		//t24 *RHt24
	    		array_X[i][15] = original_data[i][7] * original_data[i][10];
	    		//t168 * RHt168
	    		array_X[i][16] = original_data[i][8] * original_data[i][11];
	    	
	    }
	    
//	    int counter = 0;
//	    for(int i = 0; i < array_X.length; i++){
//	    	if(array_X[i][0]==0){
//	    		counter++;
//	    	}
//	    }
//	    
//	    double[][] arr_X = new double[array_X.length - counter][17];
//	    for(int i = 0, k =0; i < array_X.length && k < arr_X.length; i++){
//	    	if(array_X[i][0]!=0){
//	    		arr_X[k] = array_X[i];
//	    		k++;
//	    	}
//	    }
//	    
//	    
//	    double[] arr_Y = new double[array_Y.length - counter];
//	    for(int i = 0, k =0; i < array_Y.length && k < arr_Y.length; i++){
//	    	if(array_Y[i]!=0){
//	    		arr_Y[k] = array_Y[i];
//	    		k++;
//	    	}
//	    }
	    
	    
//	    Matrix Y = new Matrix(array_Y, array_Y.length);
	    
//	    double[] test_Y = Y.getRowPackedCopy();
	    
//	    System.out.println(Arrays.toString(test_Y));
	    
	    
	    
//	    Utility.printArray(array_X, 0, array_X.length);
//	    
//	    System.out.println(Arrays.toString(array_Y));

	   
//	    
	    int length = array_Y.length;
	    
	    Matrix weight = Matrix.identity(length, length);
		
		Regression regression = new Regression();
		
		weight = regression.getIterWeight(array_Y, array_X, weight);
		
//		Utility.printArraySingleLine(weight.getArray());
		
		Matrix cofficient = regression.getWeightedCoefficient(array_Y, array_X, weight);
		
		double[][] temp_array = cofficient.getArray();
		
		
		
		System.out.print("Hour: " + hour+" b: ");
		Utility.printArray(temp_array);
		
		return cofficient;
	}
	
	public Matrix T_24_168_2011(int hour) throws IOException{
		ReadExcel Original = new ReadExcel();
		Original.setInputFile("c:/CoolingLoadProject/All result1.xls");
		
	    double[][] original_data = Original.readT24_168(hour);
	    
	    int length_origianl = original_data.length;
	    double[] array_Y = new double[214];
	    double[][] array_X = new double[214][17];
	    
	    
	    
	    for(int i = 5140+hour, k = 0; i < 10276&&k<214; i=i+24, k++){
	    	array_Y[k] = original_data[i][0];
	    	for(int j = 0; j < 11; j++){	
	    		array_X[k][j] = original_data[i][j+1];
	    	}
	    }
	    
	    for(int i = 5140+hour, k = 0; i < 10276&&k<214; i=i+24, k++){
	    		//t*t
	    		array_X[k][11] = original_data[i][6] * original_data[i][6];
	    		//t24*t24
	    		array_X[k][12] = original_data[i][7] * original_data[i][7];
	    		//t168*t168
	    		array_X[k][13] = original_data[i][8] * original_data[i][8];
	    		//t * RHt
	    		array_X[k][14] = original_data[i][6] * original_data[i][9];
	    		//t24 *RHt24
	    		array_X[k][15] = original_data[i][7] * original_data[i][10];
	    		//t168 * RHt168
	    		array_X[k][16] = original_data[i][8] * original_data[i][11];
	    	
	    }
	    
	    
	    
//	    Matrix Y = new Matrix(array_Y, array_Y.length);
	    
//	    double[] test_Y = Y.getRowPackedCopy();
	    
//	    System.out.println(Arrays.toString(test_Y));
	    
	    
	    
//	    Utility.printArray(array_X, 0, array_X.length);
//	    
//	    System.out.println(Arrays.toString(array_Y));

	   
//	    
	    int length = array_Y.length;
	    
	    Matrix weight = Matrix.identity(length, length);
		
		Regression regression = new Regression();
		
		weight = regression.getIterWeight(array_Y, array_X, weight);
		
//		Utility.printArraySingleLine(weight.getArray());
		
		Matrix cofficient = regression.getWeightedCoefficient(array_Y, array_X, weight);
		
		double[][] temp_array = cofficient.getArray();
		
//		Utility.printArray(temp_array);
		
		System.out.print("2011, Hour: " + hour+" b: ");
		Utility.printArray(temp_array);
		
		return cofficient;
	}
	
	public Matrix T_24_168_2012(int hour) throws IOException{
		ReadExcel Original = new ReadExcel();
		Original.setInputFile("c:/CoolingLoadProject/All result1.xls");
		
	    double[][] original_data = Original.readT24_168(hour);
	    
	    int length_origianl = original_data.length;
	    double[] array_Y = new double[214];
	    double[][] array_X = new double[214][17];
	    
	    
	    
	    for(int i = 10276+hour, k = 0; i < 15412&&k<214; i=i+24, k++){
	    	array_Y[k] = original_data[i][0];
	    	for(int j = 0; j < 11; j++){	
	    		array_X[k][j] = original_data[i][j+1];
	    	}
	    }
	    
	    for(int i = 10276+hour, k = 0; i < 15412&&k<214; i=i+24, k++){
	    		//t*t
	    		array_X[k][11] = original_data[i][6] * original_data[i][6];
	    		//t24*t24
	    		array_X[k][12] = original_data[i][7] * original_data[i][7];
	    		//t168*t168
	    		array_X[k][13] = original_data[i][8] * original_data[i][8];
	    		//t * RHt
	    		array_X[k][14] = original_data[i][6] * original_data[i][9];
	    		//t24 *RHt24
	    		array_X[k][15] = original_data[i][7] * original_data[i][10];
	    		//t168 * RHt168
	    		array_X[k][16] = original_data[i][8] * original_data[i][11];
	    	
	    }
	    
	    
	    
//	    Matrix Y = new Matrix(array_Y, array_Y.length);
	    
//	    double[] test_Y = Y.getRowPackedCopy();
	    
//	    System.out.println(Arrays.toString(test_Y));
	    
	    
	    
//	    Utility.printArray(array_X, 0, array_X.length);
//	    
//	    System.out.println(Arrays.toString(array_Y));

	   
//	    
	    int length = array_Y.length;
	    
	    Matrix weight = Matrix.identity(length, length);
		
		Regression regression = new Regression();
		
		weight = regression.getIterWeight(array_Y, array_X, weight);
		
//		Utility.printArraySingleLine(weight.getArray());
		
		Matrix cofficient = regression.getWeightedCoefficient(array_Y, array_X, weight);
		
		double[][] temp_array = cofficient.getArray();
		
//		Utility.printArray(temp_array);
		
		System.out.print("2012, Hour: " + hour+" b: ");
		Utility.printArray(temp_array);
		
		return cofficient;
	}
	
	
	
	
	public static void main(String[] args) throws IOException{
		TestingExcel test =  new TestingExcel();
		
//		test.T_24_168_2010(0);
		for(int i=0; i < 24; i++){
			test.T_24_168_2010(i);
		}
//		
//		for(int i=0; i < 24; i++){
//			test.T_24_168_2011(i);
//		}
//		
//		for(int i=0; i < 24; i++){
//			test.T_24_168_2012(i);
//		}
	}
	
}
